/**
 * 公用方法
 * Created by zengtao on 2017/8/28.
 */
define(["jquery", "artTemplate","layer"], function ($, art,layer) {
	$.ajaxSetup({
		async: false,
		timeout:12000,
		global:true
	});
	var getDataBegin;
	$(document).ajaxStart(function() {
		getDataBegin = layer.load(4, {
			time: 12 * 1000
		});
		$(".commit_btn").prop('disabled', true).addClass("waiting");
	});
	$(document).ajaxComplete(function(event, request, settings) {
		 if(request.statusText != "OK"){
			 layer.alert("网络异常，请刷新重试!")
		 }
		 layer.close(getDataBegin);
		 $(".commit_btn").prop('disabled', false).removeClass("waiting");
	});
	return {
		/**
		 * 设置时间控件的时间为30天前
		 * @param 
		 */
		lastMonthDay: function () {
			var timenow = new Date();
			timenow.setDate(timenow.getDate() - 30);
			var y = timenow.getFullYear(); //年
			var m = timenow.getMonth() + 1; //月
			m = m < 10 ? '0' + m : m;
			var d = timenow.getDate(); //日
			d = d < 10 ? ('0' + d) : d;
			var time = y + '-' + m + '-' + d; 
			return time;
		},

		/**
		 * 设置时间控件的时间为本月1号
		 * @param 
		 */
		monthFirstDay: function () {
			var timenow = new Date();
			timenow.setDate(timenow.getDate());
			var y = timenow.getFullYear(); //年
			var m = timenow.getMonth() + 1; //月
			m = m < 10 ? '0' + m : m;
			var time = y + '-' + m + '-' + '01'; 
			return time;
		},

		/**
		 * 设置时间控件的时间为当天：年-月-日
		 * @param 
		 */
		dateNowYmd: function () {
			var timenow = new Date();
			timenow.setDate(timenow.getDate());
			var y = timenow.getFullYear(); //年
			var m = timenow.getMonth() + 1; //月
			m = m < 10 ? '0' + m : m;
			var d = timenow.getDate(); //日
			d = d < 10 ? ('0' + d) : d;
			var time = y + '-' + m + '-' + d; 
			return time;
		},

		/**
		 * 获取当前时间 年月日时分秒
		 * @param 
		 */
		dateNowYmdhms: function () {
			var date = new Date();
			var seperator1 = "-";
			var seperator2 = ":";
			var month = date.getMonth() + 1;
			var strDate = date.getDate();
			if (month >= 1 && month <= 9) {
				month = "0" + month;
			}
			if (strDate >= 0 && strDate < 10) {
				strDate = "0" + strDate;
			}
			var h = date.getHours();
			if (h >= 0 && h <= 9) {
				h = "0" + h;
			}
			
			var m = date.getMinutes();
			if (m >= 0 && m <= 9) {
				m = "0" + m;
			}
			var s = date.getSeconds();
			if (s >= 0 && s <= 9) {
				s = "0" + s;
			}

			var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate +
				" " + h + seperator2 + m +
				seperator2 + s;
			//console.log(currentdate)
			return currentdate;
		},
		cloneOptionStr: function (arr) {
			var strO = '<option value="">请选择</option>';
			$(arr).each(function (i, v) {
				strO += '<option value="' + v.yf_id + '" data-yfid="' + v.yf_code + '">' + v.yf_name + '</option>'
			})
			return strO;
		},
		cloneOptionStr2: function (arr) { //添加药品属性
			var strO = "";
			$(arr).each(function (i, v) {
				strO += '<div class="checkbox">' +
					'<label>' +
					'<input type="checkbox" class="ypsxcheckbox" data-yfid="' + v.yf_code + '" name="yf_drug_attribute_id" value="' + v.yf_id + '">' + v.yf_name +
					'</label>' +
					'</div>'
			})
			return strO;
		},
		/**
		 * 获取用药途径
		 * @param ele 需要填充的select选择器
		 */
		getYytj: function (ele) {
			var that = this;
			$.post(that.kcUrl() + "medicineinfo/medicineParam.shtml", {
				"type": "1"
			}, function (res) {
				var result = eval('(' + res.result + ')');
				$(ele).html(that.cloneOptionStr(result));
			})
		},
		/**
		 * 获取药品属性
		 * @param ele 需要填充的容器
		 */
		getYpsx: function (ele) {
			var that = this;
			$.post(that.kcUrl() + "medicineinfo/medicineParam.shtml", {
				"type": "2"
			}, function (res) {
				var result = eval('(' + res.result + ')');
				$(ele).html(that.cloneOptionStr2(result));
			})
		},
		/**
		 * 获取药品用法
		 * @param ele 需要填充的容器
		 */
		getYpyf: function (ele) {
			var that = this;
			$.post(that.kcUrl() + "medicineinfo/medicineParam.shtml", {
				"type": "3"
			}, function (res) {
				var result = eval('(' + res.result + ')');
				$(ele).html(that.cloneOptionStr(result));
			})
		},
		/**
		 * 获取用药间隔
		 * @param ele 需要填充的select选择器
		 */
		getYyjg: function (ele) {
			var that = this;
			$.post(that.kcUrl() + "medicineinfo/medicineParam.shtml", {
				"type": "4"
			}, function (res) {
				var result = eval('(' + res.result + ')');
				$(ele).html(that.cloneOptionStr(result));
			})
		},
		/**
		 * 获取药品分类(药品类别)
		 * @param ele 需要填充的select选择器
		 */
		getYyfl: function (ele) {
			var that = this;
			var str = '<option value="">不限</option>';
			$.post(that.kcUrl() + "medicineinfo/medicineParam.shtml", {
				"type": "5"
			}, function (res) {
				var result = eval('(' + res.result + ')');
				$(result).each(function(i,v){
					str += '<option value="'+v.yf_id+'">'+v.yf_name+'</option>'
				})
				$(ele).html(str);
			})
		},
		getYplb: function (ele) {//药品类别
			var that = this;
			var str = '<option value="">不限</option>';
			$.post(that.kcUrl() + "medicineinfo/medicineParam.shtml", {
				"type": "5"
			}, function (res) {
				var result = eval('(' + res.result + ')');
				$(result).each(function(i,v){
					str += '<option value="'+v.yf_id+'">'+v.yf_name+'</option>'
				})
				$(ele).html(str);
			})
		},
		/**
		 * 获取可添加的药品列表
		 * @param ele 需要填充的select选择器
		 */
		getIsAddDragList: function (searchkey) {
			var that = this,
				result;
			$.post(that.kcUrl() + "medicineinfo/medicineDicList.shtml", {
				"searchkey": ""
			}, function (res) {
				result = eval('(' + res.result + ')');
			})
			return result;
		},
		/**
		 * 获取主机地址
		 */
		kcUrl: function () {
			var url = window.location.protocol + "//" + window.location.host + "/" + "yunis/";
			return url;
		},
		/**
		 * 获取操作人姓名和ID
		 * @param selTagId 下拉选择框ID "#XX"
		 */
		getOp: function (selTagId, url) {
			//获取操作人名字和ID
			var str = "";
			$.post(this.kcUrl() + "base/operatorList.shtml", {}, function (res) {
				var result = eval('(' + res.result + ')');
				for (var i = 0; i < result.length; i++) {
					str += "<option value='" + result[i].user_id + "'>" + result[i].user_name + "</option>"
					//$(selTagId).append("<option value='" + res.result[i].user_id + "'>" + res.result[i].user_name + "</option>");
				}
				$(selTagId).html(str);
				$(selTagId).children("option[value=" + $('#current_userid').val() + "]").attr("selected", "selected"); //设置为当前登录操作人
			});
		},
		/**
		 * 复制操作人姓名和ID
		 * @param ID 待填充的下拉选择框ID "#XX"
		 */
		cloneOp: function (id) {
			$("#select_operator_list option").each(function () {
				var oValue = $(this).val().toString();
				var oText = $(this).text().toString();
				$(id).append("<option value='" + oValue + "'>" + oText + "</option>");
			});
			$(id).children("option[value=" + $('#current_userid').val() + "]").attr("selected", "selected"); //设置为当前登录操作人
		},

		/**
		 * 复制物品类型和ID
		 * @param ID 待填充的下拉选择框ID "#XX"
		 */
		cloneType: function (id) {
			return false
			$("#select_wptype_list option").each(function () {
				var oValue = $(this).val().toString();
				var oText = $(this).text().toString();
				$(id).append("<option value='" + oValue + "'>" + oText + "</option>");
			});
		},

		/**
		 * 复制经销商和ID
		 * @param ID 待填充的下拉选择框ID "#XX"
		 */
		cloneJxs: function (id) {
			return false
			$("#select_jingxiaos_list option").each(function () {
				var oValue = $(this).val().toString();
				var oText = $(this).text().toString();
				$(id).append("<option value='" + oValue + "'>" + oText + "</option>");
			});
		},

		/**
		 * 复制厂家和ID
		 * @param ID 待填充的下拉选择框ID "#XX"
		 */
		cloneCj: function (id) {
			return false
			$("#select_changjia_list option").each(function () {
				var oValue = $(this).val().toString();
				var oText = $(this).text().toString();
				$(id).append("<option value='" + oValue + "'>" + oText + "</option>");
			});
		},

		/**
		 * 更新物品类型
		 * @param 
		 */
		getwuptype: function () {
			return false
			$.get(this.kcUrl() + "inventory/inventorytypeList.shtml", function (res) {
				$("#select_wptype_list").empty();
				var result = eval('(' + res.result + ')');
				for (var i = 0; i < result.length; i++) {
					$("#select_wptype_list").append("<option value='" + result[i].sk_type_id + "'>" + result[i].sk_type_name + "</option>");
				}
			});
		},

		/**
		 * 更新生产厂家列表
		 * @param 
		 */
		getUpCj: function () {
			return false
			//保存生产厂家列表
			$.post(this.kcUrl() + "inventory/providerList.shtml", {
				type: 0
			}, function (res) {
				var result = eval('(' + res.result + ')');
				$("#select_changjia_list").empty().append("<option value=''> </option>");
				for (var i = 0; i < result.length; i++) {
					$("#select_changjia_list").append("<option value='" + result[i].sk_provider_id + "'>" + result[i].sk_provider_name + "</option>");
				}
			});
		},

		/**
		 * 更新供应商
		 * @param 
		 */
		getUpGys: function () {
			return false
			//保存供应商列表
			$.post(this.kcUrl() + "ypgl/tpls/yaopin-pianji.json?v=2", {
				type: 1
			}, function (res) {
				var result = eval('(' + res.result + ')');
				$("#select_jingxiaos_list").empty().append("<option value=''> </option>");
				for (var i = 0; i < result.length; i++) {
					$("#select_jingxiaos_list").append("<option value='" + result[i].sk_provider_id + "'>" + result[i].sk_provider_name + "</option>");
				}
			});
		},

		

		/**
		 * 库存查询模块，请求数据后弹窗显示相应内容
		 * @param url
		 * @param sendData 示例 {inventory_id:inventory_id,batch:batch,type:0}
		 * @param htmlTpl 需要渲染的模板id  "#xx"
		 * @param layerTitle 弹窗标题
		 * @param tag1 详细内容部分容器的ul 选择器
		 * @param provider_id 物品编码
		 * @param provider_name 物品名称
		 */
		openLayer: function (url, sendData, htmlTpl, layerTitle, tag1, provider_id, provider_name) {
			//发送请求物品详情
			$.get(this.kcUrl() + url, sendData, function (res) {
				//console.log(res);
				var result = eval('(' + res.result + ')');
				//console.log(result);
				var listHtml = art.render($(htmlTpl).html(), {
					result: result
				});
				layer.open({
					title: [layerTitle, 'font-size:18px;background:#EFF2F5;'],
					type: 1,
					shadeClose: true,
					id: "layer_open",
					area: ['1032px', '616px'],
					resize: false,
					content: listHtml
				});
				$("#sk_provider_id").text(provider_id);
				$("#sk_provider_name").text(provider_name);
				if (result.length == 0) {
					$("#wprkxq_list_ul_svf").empty().append("<li class='list_empty'><span>暂无数据</span></li>");
					return
				}
				setPaddingR(tag1, ".tip_layer_show");
			});
		},

		openLayerPre: function (url, sendData, htmlTpl, layerTitle, tag1, provider_id, provider_name) {
			//发送请求物品详情
			$.get(this.kcUrl() + url, sendData, function (res) {
				//console.log(res);
				var result = eval('(' + res.result + ')');
				//console.log(result);
				var listHtml = art.render($(htmlTpl).html(), {
					result: result
				});
				layer.open({
					title: [layerTitle, 'font-size:18px;background:#EFF2F5;'],
					type: 1,
					shadeClose: true,
					id: "layer_open",
					area: ['1032px', '616px'],
					resize: false,
					content: listHtml
				});
				$("#sk_provider_id").text(provider_id);
				$("#sk_provider_name").text(provider_name);
				if (result.length == 0) {
					$("#wprkxq_list_ul_svf").empty().append("<li class='list_empty'><span>暂无数据</span></li>");
					return
				}
				setPaddingR(tag1, ".tip_layer_show");
				$("#kcpd_pdls_ul_svc").find("span.kcpd_pdls_pdzj").each(function (v, i) { //将金额为-0的显示为0
					var pdzj = $(this).text();
					if (Math.abs(pdzj) == 0) {
						$(this).text("0");
					}
				});
			});
		},

		/**
		 * 价格保留2位小数
		 * @param price 用户输入的价格
		 */
		float_num2: function (price) {
			var f_x = parseFloat(price);
			if (isNaN(f_x)) {
				return 0;
			}
			var f_x = Math.round(price * 100) / 100;
			var s_x = f_x.toString();
			var pos_decimal = s_x.indexOf('.');
			if (pos_decimal < 0) {
				pos_decimal = s_x.length;
				s_x += '.';
			}
			while (s_x.length <= pos_decimal + 2) {
				s_x += '0';
			}
			return s_x;
		}


	};
});